package com.apps.smodule.model.po;

import com.apps.sannotation.constraints.NoRepeat;
import com.apps.shelper.ClassHelper;
import com.apps.shelper.FieldHelper;
import com.apps.shelper.enums.ClassType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.models.links.Link;
import lombok.Data;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

@JsonInclude(Include.NON_NULL)
@ApiModel
//@Data
public class ApiResult<T> implements Serializable {

	private static final long serialVersionUID = -8810954049370861490L;

	@ApiModelProperty(value = "状态码")
	public int status;
	@ApiModelProperty(value = "响应结果")
	public String msg;
	@ApiModelProperty(value = "系统当前时间")
	public long time;
	@ApiModelProperty(value = "返回数据")
	public T result;
	private T t;

	public ApiResult() {
		super();
	}

	public ApiResult(int status, String msg, T result) {
		super();
		this.status = status;
		this.msg = msg;
		this.result = result;
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
	}

	public ApiResult(T result) {
		super();
		this.status = Ar.SUCCESS.ordinal();
		this.msg = Ar.SUCCESS.getMsg();
		
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		this.result = result;
	}

	public ApiResult<T> success(T result) {
		this.status = Ar.SUCCESS.ordinal();
		this.msg = Ar.SUCCESS.getMsg();
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		this.result = result;
		return this;
	}

	public ApiResult<T> success() {
		this.status = Ar.SUCCESS.ordinal();
		this.msg = Ar.SUCCESS.getMsg();
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> succMsg(String msg) {
		this.status = Ar.SUCCESS.ordinal();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> dataError() {
		this.status = Ar.DATAERROR.ordinal();
		this.msg = Ar.DATAERROR.getMsg();
		return this;
	}

	public ApiResult<T> dataError(String msg) {
		this.status = Ar.DATAERROR.ordinal();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		return this;
	}

	public ApiResult<T> dataError(int error,String msg) {
		this.status = error;
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		return this;
	}

	public ApiResult<T> failMsg(String msg) {
		this.status = Ar.FAIL.ordinal();
		if (null == this.msg) {
			this.msg = msg;
		} else {
			this.msg = msg;
		}
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> fail(String msg) {
		this.status = Ar.FAIL.ordinal();
		this.msg = msg;
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> fail() {
		this.status = Ar.FAIL.ordinal();
		this.msg = Ar.FAIL.getMsg();
		this.time=Calendar.getInstance().getTimeInMillis()/1000;
		return this;
	}

	public ApiResult<T> unauthorized() {
		this.status = Ar.NOLOGIN.ordinal();
		this.msg = Ar.NOLOGIN.getMsg();
		return this;
	}
	
	public ApiResult<T> invalid() {
		this.status = Ar.INVALID.ordinal();
		this.msg = Ar.INVALID.getMsg();
		return this;
	}

	public String toStr() {
		return "{\"status\":" + status + ",\"msg\":\"" + msg + "\"}";
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public long getTime() {
		return time;
	}

	public void setTime(long time) {
		this.time = time;
	}

	public T getResult() {
		if(null != this.result)
		   this.handleResult(this.result);
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

	/**
	 * 处理对象的为空情况,自动填充默认值
	 * @param _result
	 */
	private void handleResult(Object _result)
	{
        if(null ==_result)
        	return;
		Object data=_result;
		if(ClassHelper.getClassType(_result.getClass()) == ClassType.LIST)
			data=((ArrayList)_result).toArray();
		if(data.getClass().isArray())
		{
			int length = Array.getLength(data);
			for (int i =0 ; i<length; i++) {
				Object item=Array.get(data, i);
				if(!item.getClass().isArray())
                    this.handleObject(item);
				else
					handleResult(item);
			}
		}else
           this.handleObject(_result);
	}

	/**
	 * 处理具体的对象,必须是非数组
	 * @param _result
	 */
	private void handleObject(Object _result)
	{
		if(null == _result)
			return;
		try {
			Field[] fields = _result.getClass().getDeclaredFields();
			for (Field field : fields) {
				field.setAccessible(true);
				Object obj=field.get(_result);
				if(null==obj)
					FieldHelper.InitDefaultValue(field,_result,"",0);

			}
		}catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
